/**
 * 主题切换工具函数
 */

export type ThemeMode = 'light' | 'dark' | 'auto';

/**
 * 获取当前主题模式
 */
export const getCurrentTheme = (): ThemeMode => {
  const stored = localStorage.getItem('theme-mode');
  if (stored && ['light', 'dark', 'auto'].includes(stored)) {
    return stored as ThemeMode;
  }
  return 'auto';
};

/**
 * 设置主题模式
 */
export const setThemeMode = (mode: ThemeMode): void => {
  localStorage.setItem('theme-mode', mode);
  applyTheme(mode);
};

/**
 * 应用主题
 */
export const applyTheme = (mode: ThemeMode): void => {
  const root = document.documentElement;

  // 移除现有的主题类
  root.classList.remove('light', 'dark');

  if (mode === 'auto') {
    // 自动模式：根据系统偏好设置
    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    root.classList.add(prefersDark ? 'dark' : 'light');
  } else {
    // 手动模式
    root.classList.add(mode);
  }
};

/**
 * 切换主题
 */
export const toggleTheme = (): ThemeMode => {
  const current = getCurrentTheme();
  let next: ThemeMode;

  switch (current) {
    case 'light':
      next = 'dark';
      break;
    case 'dark':
      next = 'auto';
      break;
    case 'auto':
    default:
      next = 'light';
      break;
  }

  setThemeMode(next);
  return next;
};

/**
 * 初始化主题
 */
export const initTheme = (): void => {
  const mode = getCurrentTheme();
  applyTheme(mode);

  // 监听系统主题变化
  const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
  const handleChange = () => {
    if (getCurrentTheme() === 'auto') {
      applyTheme('auto');
    }
  };

  mediaQuery.addEventListener('change', handleChange);
};

/**
 * 获取主题变量值
 */
export const getThemeVar = (varName: string): string => {
  return getComputedStyle(document.documentElement).getPropertyValue(varName).trim();
};

/**
 * 设置主题变量值
 */
export const setThemeVar = (varName: string, value: string): void => {
  document.documentElement.style.setProperty(varName, value);
};

/**
 * 获取当前是否为暗色主题
 */
export const isDarkTheme = (): boolean => {
  return document.documentElement.classList.contains('dark');
};

/**
 * 主题变量名称映射
 */
export const themeVarNames = {
  // 颜色变量
  primary: '--primary-color',
  success: '--success-color',
  warning: '--warning-color',
  error: '--error-color',
  info: '--info-color',

  // 背景变量
  layout: '--layout-bg-color',
  container: '--container-bg-color',
  inverted: '--inverted-bg-color',

  // 文本变量
  baseText: '--base-text-color',

  // 阴影变量
  headerShadow: '--header-box-shadow',
  siderShadow: '--sider-box-shadow',
  tabShadow: '--tab-box-shadow',
} as const;

/**
 * 获取主题颜色 RGB 值
 */
export const getThemeColor = (colorName: keyof typeof themeVarNames): string => {
  const varName = themeVarNames[colorName];
  const rgbValue = getThemeVar(varName);
  return `rgb(${rgbValue})`;
};

/**
 * 主题预设配置
 */
export const themePresets = {
  default: {
    primary: '0, 101, 242',
    success: '34, 197, 94',
    warning: '245, 158, 11',
    error: '239, 68, 68',
    info: '59, 130, 246',
  },
  blue: {
    primary: '59, 130, 246',
    success: '34, 197, 94',
    warning: '245, 158, 11',
    error: '239, 68, 68',
    info: '59, 130, 246',
  },
  green: {
    primary: '34, 197, 94',
    success: '34, 197, 94',
    warning: '245, 158, 11',
    error: '239, 68, 68',
    info: '59, 130, 246',
  },
  purple: {
    primary: '147, 51, 234',
    success: '34, 197, 94',
    warning: '245, 158, 11',
    error: '239, 68, 68',
    info: '59, 130, 246',
  },
} as const;

/**
 * 应用主题预设
 */
export const applyThemePreset = (presetName: keyof typeof themePresets): void => {
  const preset = themePresets[presetName];

  Object.entries(preset).forEach(([colorName, rgbValue]) => {
    const varName = themeVarNames[colorName as keyof typeof themeVarNames];
    if (varName) {
      setThemeVar(varName, rgbValue);
    }
  });
};
